"""
====================================================================
Copyright (C) 2012-2013 Eric Blond

This file is part of Neptune.

Neptune is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Neptune is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Neptune.  If not, see <http://www.gnu.org/licenses/>.
====================================================================

Created on 18 Dec 2012

@author: Eric
"""

import wx
from ..nucleus import public as nucleus
from .enums import AlignEnum
from . import context

#######

class GridDecorator(nucleus.Page):

    Border   = 0
    Align    = AlignEnum.TOP | AlignEnum.LEFT
    Position = None
    Expand   = False
    HSpan    = 1
    VSpan    = 1
    Element  = None

#######

def translate_align(value):
    result = 0
    if value & AlignEnum.LEFT:
        result |= wx.ALIGN_LEFT
    elif value & AlignEnum.RIGHT:
        result |= wx.ALIGN_RIGHT
    if value & AlignEnum.TOP:
        result |= wx.ALIGN_TOP
    elif value & AlignEnum.BOTTOM:
        result |= wx.ALIGN_BOTTOM
    if value & AlignEnum.CENTER:
        result |= wx.ALIGN_CENTER
    elif value & AlignEnum.CENTER_HORIZ:
        result |= wx.ALIGN_CENTER_HORIZONTAL
    elif value & AlignEnum.CENTER_VERT:
        result |= wx.ALIGN_CENTER_VERTICAL
    return result

#######

class GridLayout(nucleus.Page):
    
    _WxPanel = None
    _WxSizer = None
    _Context = None

    HGap = 0
    VGap = 0
    
    Matrix = []

    @nucleus.cell
    def _WxWindow(self):
        return self._WxPanel

    def _add(self, item, row, column):
        if not isinstance(item, GridDecorator):
            item = GridDecorator(Element = item)
        item.Element._create(self + self._Context)
        flags = 0
        if item.Position:
            row, column = item.Position
        flags |= translate_align(item.Align)
        if item.Expand:
            flags |= wx.EXPAND
        self._WxSizer.Add(item = item._WxWindow,
                          pos = (row, column),
                          span = (item.HSpan, item.VSpan),
                          flag = flags,
                          border = item.Border)
    
    def _walk(self, function):
        row, column = 0, 0
        for item in self.Matrix:
            if isinstance(item, (list, tuple)):
                for item2 in item:
                    function(item2, row, column)
                    column += 1
            elif item:
                function(item, row, column)
            row += 1
            column = 0

    def _create(self, ctx):
        self._Context = context.Context(Parent = ctx, Instance = self)
        self._WxPanel = wx.Panel(self._Context._WxWindow, wx.ID_ANY)
        self._WxSizer = wx.GridBagSizer(self.HGap, self.VGap)
        self._WxPanel.SetSizer(self._WxSizer)
        self._walk(self._add)
    
    def _destroy(self):
        self._walk(lambda x, y, z: x._destroy())
        self._WxPanel.Destroy()
        self._WxPanel = None
        self._WxSizer = None
        self._Context = None
